<template>
  <div class="user-container">
    <el-row :gutter="20">
      <el-col :span="12">
        <el-input
          placeholder="请输入内容"
          class="input-with-select"
          v-model="queryInfo.query"
          clearable
          @clear="onClearProc"
        >
          <el-button
            slot="append"
            icon="el-icon-search"
            @click="onSearch"
          ></el-button>
        </el-input>
      </el-col>
      <el-col :span="12">
        <add-dialog @transferUser="addUsered"></add-dialog>
      </el-col>
    </el-row>
    <!--#region 表格数据 -->
    <el-table :data="userList" border stripe>
      <el-table-column type="index"></el-table-column>
      <el-table-column prop="userName" label="用户"> </el-table-column>
      <el-table-column prop="email" label="邮箱"> </el-table-column>
      <el-table-column prop="phone" label="电话"> </el-table-column>
      <el-table-column prop="roleName" label="角色"> </el-table-column>
      <el-table-column prop="state" label="状态" width="120px">
        <template slot-scope="scope">
          <div class="center">
            <el-switch
              v-model="scope.row.state"
              @change="userStateChange(scope.row)"
            ></el-switch>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <div class="center">
            <ps-putuser :user="scope.row"></ps-putuser>
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              @click="deleteUser(scope.row)"
            ></el-button>
            <el-tooltip
              class="item"
              effect="dark"
              content="分配角色"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="warning"
                icon="el-icon-setting"
                circle
              ></el-button>
            </el-tooltip>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- #endregion -->

    <!-- #region 分页-->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pageIndex"
      :page-sizes="[1, 2, 5, 10, 15, 100]"
      :page-size="queryInfo.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <!-- #endregion -->
  </div>
</template>

<script>
import DialogAdd from "./User/AddUser.vue";
import PutUser from "./User/PutUser";
export default {
  components: {
    "add-dialog": DialogAdd,
    "ps-putuser": PutUser,
  },
  data() {
    return {
      userList: [],
      queryInfo: {
        query: "",
        // 当前页数
        pageIndex: 1,
        // 当前页数据大小
        pageSize: 5,
      },
      total: 0,
    };
  },
  created() {
    this.getUserList();
  },
  methods: {
    async onSearch() {
      this.getUserList();
    },
    /**
     * 获取用户列表
     * @param {Type} none
     * @returns  void
     */
    async getUserList() {
      let response = await this.$myhttp.get("UserRoles", this.queryInfo);
      console.log(response);
      if (response.status == 200) {
        this.userList = response.data.userList.sort();
        this.total = response.data.total;
      }
    },
    /**
     * 每页数据大小变化时调用
     * @param newPageSize 新的页面大小
     * @returns  none
     */
    handleSizeChange(newPageSize) {
      console.log(newPageSize);
      this.queryInfo.pageSize = newPageSize;
      this.getUserList();
    },
    // 页码变化
    handleCurrentChange(newPageIndx) {
      console.log(newPageIndx);
      this.queryInfo.pageIndex = newPageIndx;
      this.getUserList();
    },
    // 用户可用状态
    async userStateChange(userInfo) {
      // console.log(userInfo);
      let a = await this.$myhttp.patch("User", userInfo.userName, userInfo);
      console.log({ a });
    },
    /**
     * 点击清理的回调函数
     * @param none
     * @returns  none
     */
    onClearProc() {
      this.getUserList();
    },
    test(url, ...args) {
      console.log(url);
      console.log(args);
    },
    addUsered(user) {
      this.userList = [user];
      this.total = 1;
    },
    async deleteUser(user) {
      await this.$confirm("确认关闭？");
      await this.$myhttp.Delete("UserRoles", user.userName, user);
      this.getUserList();
    },
  },
};
</script>

<style lang="less" scoped>
.user-container {
  background-color: #fff;
  height: 100%;
  .el-table {
    margin-top: 15px;
    .center {
      display: flex;
      justify-content: space-between;
      padding: 0 1vw;
      .el-tooltip {
        margin-left: 0;
      }
    }
  }
  .el-pagination {
    margin-top: 15px;
  }
}
</style>
